<?php
session_start ();

require_once ('Facebook/FacebookSession.php');

require_once ('Facebook/FacebookRedirectLoginHelper.php');

require_once ('Facebook/FacebookRequest.php');

require_once ('Facebook/FacebookResponse.php');

require_once ('Facebook/FacebookSDKException.php');

require_once ('Facebook/FacebookRequestException.php');

require_once ('Facebook/FacebookAuthorizationException.php');

require_once ('Facebook/GraphObject.php');

require_once ('Facebook/GraphUser.php');

require_once ('Facebook/GraphSessionInfo.php');

use Facebook\FacebookSession;

use Facebook\FacebookRedirectLoginHelper;

use Facebook\FacebookRequest;

use Facebook\FacebookResponse;

use Facebook\FacebookSDKException;

use Facebook\FacebookRequestException;

use Facebook\FacebookAuthorizationException;

use Facebook\GraphObject;

use Facebook\GraphUser;

use Facebook\GraphSessionInfo;

$id = '700715950012686';

$secret = 'a33a75e4bafc2b72b52e12d49a49a594';

// init app with app id and secret
FacebookSession::setDefaultApplication ( $id, $secret );

$helper = new FacebookRedirectLoginHelper ( 'http://localhost/bootstrap2/index.php' );

try {	
	$session = $helper->getSessionFromRedirect ();
} catch ( FacebookRequestException $ex ) {
	// When Facebook returns an error
} catch ( Exception $ex ) {
	// When validation fails or other local issues
	echo 'local issue!';
}

// nhap
/*
if (isset ( $session )) {
	$request = new FacebookRequest ( $session, 'GET', 'me' );
	$response = $request->execute ();
	// get response
	$graphObject = $response->getGraphObject ();
	
	// print data
	echo '<pre>' . print_r ( $graphObject, 1 ) . '</pre>';
} else {
	// show login url
	echo '<a href="' . $helper->getLoginUrl () . '">Login</a>';
}*/
// ----------

if (isset ( $_SESSION ['token'] )) {
	echo "1";
	$session = new FacebookSession ( $_SESSION ['token'] );
	
	try {
		$session->Validate ( $id, $secret );
	} catch ( FacebookAuthorizationException $e ) {
		
		$session = '';
	}
}

if (isset ( $session )) {
	echo "2";
	$_SESSION ['token'] = $session->getToken ();
	
	echo "Login Successful<br>";
	
	$request = new FacebookRequest ( $session, 'GET', '/me' );
	
	$response = $request->execute ();
	
	$graph = $response->getGraphObject ( GraphUser::className () );
	
	echo "Hi " . $graph->getName ();
} else {	
	echo "3";
	echo "<a href = " . $helper->getLoginUrl () . ">Login With Facebook</a>";
}
?>

